package com.qy.controller.app;
import com.qy.base.core.*;
import com.qy.model.Label;
import com.qy.service.LabelService;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zaq on 2018/07/10.
*/
@RestController
@RequestMapping("app/label")
public class LabelController {
    @Resource
    private LabelService labelService;

    @PostMapping("/add")
    public Result add(@RequestBody Label label) {
        labelService.save(label);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        labelService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Label label) {
        labelService.update(label);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Label label = labelService.findById(id);
        return ResultGenerator.successResult(label);
    }

    @GetMapping("/list")
    public Result list(PageBean<Label> page) {
        PageHelper.startPage(page.getPageNum(),page.getSize());
        List<Label> list = labelService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //查询标签列表
    @GetMapping("/labelList")
    public Result labelList(Label label) {
        if (label.getId()==null){
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Label> list = labelService.selectChildLabel(label);
        return ResultGenerator.successResult(list);
    }
}
